package com.mpgame.database.mysql.service.game;

import com.mpgame.database.mysql.mapper.game.FamilyMapper;
import com.mpgame.database.mysql.model.game.Family;
import com.mpgame.database.mysql.utils.MyBatisUtil;
import com.mpgame.utils.Log;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FamilyService {
    
    public int createFamily(Family family) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            FamilyMapper mapper = sqlSession.getMapper(FamilyMapper.class);
            int res = mapper.insert(family);
            sqlSession.commit();
            
            if (res > 0) {
                Log.info("创建属性成功: " + family.getName());
                return family.getId();
            } else {
                Log.err("创建属性失败: " + family.getName());
                return 0;
            }
        } catch (Exception e) {
            Log.err("创建属性异常: " + e.getMessage());
            throw new RuntimeException("创建属性异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public Family getFamilyById(int id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            FamilyMapper mapper = sqlSession.getMapper(FamilyMapper.class);
            return mapper.selectById(id);
        } catch (Exception e) {
            Log.err("获取属性失败: " + e.getMessage());
            throw new RuntimeException("获取属性异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public Family getFamilyByName(String name) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            FamilyMapper mapper = sqlSession.getMapper(FamilyMapper.class);
            return mapper.selectByName(name);
        } catch (Exception e) {
            Log.err("根据名称获取属性失败: " + e.getMessage());
            throw new RuntimeException("根据名称获取属性异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public List<Family> getAllFamilies() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            FamilyMapper mapper = sqlSession.getMapper(FamilyMapper.class);
            return mapper.selectAll();
        } catch (Exception e) {
            Log.err("获取所有属性失败: " + e.getMessage());
            throw new RuntimeException("获取所有属性异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
    
    public boolean updateFamily(Family family) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MyBatisUtil.getSqlSession();
            FamilyMapper mapper = sqlSession.getMapper(FamilyMapper.class);
            int res = mapper.updateById(family);
            sqlSession.commit();
            return res > 0;
        } catch (Exception e) {
            Log.err("更新属性失败: " + e.getMessage());
            throw new RuntimeException("更新属性异常", e);
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }
}
